<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="新客户id" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入新客户id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="跟进用户id" prop="followUserId">
        <el-input
          v-model="queryParams.followUserId"
          placeholder="请输入跟进用户id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="跟进用户" prop="followUserId">
        <el-select
          v-loading="loading"
          v-model="queryParams.followUserId"
          @change="() => handleQuery()"
          placeholder="请选择跟进用户"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in followUpList"
            :key="item.id"
            :value="item.id"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="放款金额" prop="loanAmount">
        <el-input
          v-model="queryParams.loanAmount"
          placeholder="请输入放款金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="佣金" prop="commission">
        <el-input
          v-model="queryParams.commission"
          placeholder="请输入佣金"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="点数" prop="points">
        <el-input
          v-model="queryParams.points"
          placeholder="请输入点数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="放款银行" prop="bank">
        <el-select
          v-model="queryParams.bank"
          placeholder="请选择放款银行"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BANK)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="放款时间" prop="loanTime">
        <el-date-picker
          v-model="queryParams.loanTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="回款时间" prop="paymentCollectionTime">
        <el-date-picker
          v-model="queryParams.paymentCollectionTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="签约时间" prop="signTime">
        <el-date-picker
          v-model="queryParams.signTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <!-- <el-form-item label="签约合同" prop="signContract">
        <el-input
          v-model="queryParams.signContract"
          placeholder="请输入签约合同"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="押金" prop="deposit">
        <el-input
          v-model="queryParams.deposit"
          placeholder="请输入押金"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="成本费" prop="cost">
        <el-input
          v-model="queryParams.cost"
          placeholder="请输入成本费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="协助进件" prop="assist">
        <el-input
          v-model="queryParams.assist"
          placeholder="请输入协助进件"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="转介绍" prop="referral">
        <el-select
          v-model="queryParams.referral"
          placeholder="请选择转介绍"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="点评" prop="review">
        <el-input
          v-model="queryParams.review"
          placeholder="请输入点评"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:loan:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:loan:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['system:loan:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <!-- <el-table-column label="新客户id" align="center" prop="customerId" /> -->
      <!-- <el-table-column label="跟进用户id" align="center" prop="followUserId" /> -->
      <el-table-column label="跟进用户名称" align="center" prop="followUserName" min-width="110" />
      <el-table-column label="客户名称" align="center" prop="customerName" min-width="110" />
      <el-table-column label="放款金额" align="center" prop="loanAmount" />
      <el-table-column label="佣金" align="center" prop="commission" />
      <el-table-column label="点数" align="center" prop="points" />
      <el-table-column label="放款银行" align="center" prop="bank" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BANK" :value="scope.row.bank" />
        </template>
      </el-table-column>
      <el-table-column
        label="放款时间"
        align="center"
        prop="loanTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="回款时间"
        align="center"
        prop="paymentCollectionTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="签约时间"
        align="center"
        prop="signTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="签约合同" align="center" prop="signContract" />
      <!-- <el-table-column label="押金" align="center" prop="deposit" /> -->
      <el-table-column label="实际创收" align="center" prop="cost" />
      <!-- <el-table-column label="协助进件" align="center" prop="assist" /> -->
      <el-table-column label="转介绍" align="center" prop="referral">
        <template #default="scope">
          <span>{{ scope.row.referral === '1' ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="点评" align="center" prop="review" /> -->
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:loan:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:loan:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <loanForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { loanApi, loan } from '@/api/system/loan'
import loanForm from './loanForm.vue'
import { customerApi } from '@/api/system/customer'

/** 放款信息 列表 */
defineOptions({ name: 'Loan' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<loan[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  customerId: undefined,
  customerName: undefined,
  followUserId: undefined,
  followUserName: undefined,
  loanAmount: undefined,
  commission: undefined,
  points: undefined,
  bank: undefined,
  loanTime: [],
  paymentCollectionTime: [],
  signTime: [],
  signContract: undefined,
  deposit: undefined,
  cost: undefined,
  assist: undefined,
  referral: undefined,
  review: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await loanApi.getloanPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const followUpList = ref([])
function getFollowUpList() {
  return customerApi.getFollowUpList().then((data) => {
    followUpList.value = data
  })
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await loanApi.deleteloan(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除放款信息 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await loanApi.deleteloanList(checkedIds.value)
    checkedIds.value = []
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: loan[]) => {
  checkedIds.value = records.map((item) => item.id!)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await loanApi.exportloan(queryParams)
    download.excel(data, '放款信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  getFollowUpList()
})
</script>
